package lianxi;

import cn.tedu.yxc.JdbcUtils;
import org.apache.commons.lang3.RandomStringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(urlPatterns = "/goods/insert")
@MultipartConfig(location = "D:\\image",
                maxFileSize = 100 * 1024*1024,
            maxRequestSize = 100*1024*1024)
public class GoddsInsertServelet  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收文件
        String name = req.getParameter("n");
        String price = req.getParameter("p");
        Part file = req.getPart("i");
        //获取上传文件的名字
        //String name = file.getSubmittedFileName();
        String u = RandomStringUtils.randomAlphabetic(10)+System.currentTimeMillis();
        //保存文件
        file.write(u);//aaa表示保存时上传文件的名字
        String sql="INSERT INTO `goods` (`name`,`price`,`image`) VALUES(?,?,?)";
        JdbcUtils.modify(sql,name,price,u);
        resp.sendRedirect("/Yxc/goods-list.jsp");
    }
}
